*****
*Do you personally know someone who has been detained or deported for immigration-
*related reasons in the last 12 months?

cd "/Users/asadasad/Dropbox/Book - Legal Visibility/Chapters/Appendix - Pew Survey/Article/Analysis/Analysis of Pew Data/PNAS Revision/Knows Detained or Deported"

*2010 data

use "2010.dta", clear
***Rename Variables ------------------------
gen svy_year = 2010

* Case Identifier (id)

* Main Dependent Variable
tab qn32
tab qn32, nolabel
recode qn32 (1=3 "Worries a lot about deportation") (2=2 "Worries some about deportation") (3=1 "Doesn't worry much about deportation") (4=0 "Doesn't worry at all about deportation") (8/9 =.), gen(worry)
label variable worry "Deportation Worries (ordered)"
tab worry, m

recode qn32 (1/2=1 "Worries a lot/some about deportation") (3/4=0 "Doesn't worry much/at all about deportation")  (8/9 =. ), gen(worry2)
tab worry2, m
label variable worry2 "Deportation Worries (binary)"

recode qn32 (1/4=0 "Answered") (8/9=1 "Refused/don't know"), gen(worry_missing)
label variable worry_missing "Deportation Worries (missing=1, non-missing=0)"

** Main Independent Variables
*All Citizens v. All Noncitizens

tab qn9
tab qn9, nolabel
recode qn9 (1=1 "U.S. Citizens") (2=0 "Noncitizens")  (8/9 = .), gen(citizen)
label variable citizen "Are you a citizen of the US?"
tab citizen,m 
rename qn4 bornusa
rename qn79 greencard
tab citizen, m

replace citizen = 1 if bornusa==1 | bornusa==2
tab citizen, m

*All Foreign Born v. All US Born
*0 = US born, 1 = foreign born
gen nativity=.
replace nativity=0 if bornusa==1
replace nativity=0 if bornusa==2
replace nativity=1 if bornusa==3
replace nativity=. if bornusa==8
replace nativity=. if bornusa==9
label define nativity_lab 1 "Foreign Born"  0 "U.S. Born" , replace
label values nativity nativity_lab
label variable nativity "Foreign Born vs U.S. Born"
tab nativity, missing

gen missing_nativity = 0
replace missing_nativity = 1 if nativity==.
tab missing_nativity

tab citizen nativity, col m

*legal status
*1=US BORN CITIZENS
*2=NATURALIZED CITIZENS
*3=PERMANENT RESIDENTS
*4=NOT A CITZEN/RESIDENT

gen legal_status=.
replace legal_status=1 if nativity==0   
replace legal_status=2 if citizen==1 & nativity==1

replace legal_status=3 if citizen==0 & nativity==1 & greencard==1
replace legal_status=3 if citizen==. & nativity==1 & greencard==1

replace legal_status=4 if citizen==0 & nativity==1 & greencard==2
replace legal_status=4 if citizen==0 & nativity==1 & greencard==8
replace legal_status=4 if citizen==0 & nativity==1 & greencard==9
replace legal_status=4 if citizen==. & nativity==1 & greencard==8
replace legal_status=4 if citizen==. & nativity==1 & greencard==9
replace legal_status=4 if citizen==. & nativity==1 & greencard==2

label define l_status 1 "U.S.-born Citizens"  2 "Naturalized Citizens" 3 "Lawful Permanent Residents" 4 "Likely-Undocumented"  
label values legal_status l_status
label variable legal_status "Legal Status (4 level)"
tab legal_status, missing

replace citizen = 1 if legal_status==1 & citizen==.
replace citizen =1 if legal_status==2 & citizen==.
replace citizen =0 if legal_status==3 & citizen==.
replace citizen =0 if legal_status==4 & citizen==.
tab citizen, m

replace legal_status = 1 if citizen==1 & legal_status==.

tab citizen legal_status, col m


*qn33: question of interest

fre qn33
recode qn33 (1=1 "Yes") (2=0 "No")  (8/9 = .), gen(deported)
label variable deported "Personally know someone detained or deported"
fre deported

rename weight svy_weight

keep worry worry2 citizen legal_status deported svy_weight id svy_year 

save "2010_clean.dta", replace




*2011 data

use "2011.dta", clear

***Rename Variables ------------------------
* Case Identifier (id)

rename caseid id
gen svy_year = 2011

* Main Dependent Variable
	*Not available

** Main Independent Variables
*All Citizens v. All Noncitizens

fre qn84
recode qn84 (1=1 "U.S. Citizens") (2=0 "Noncitizens")  (9 = .), gen(citizen)
label variable citizen "Are you a citizen of the US?"
fre citizen 
rename qn4 bornusa
fre bornusa
rename qn97 greencard
fre greencard
tab citizen, m

replace citizen = 1 if bornusa==1 | bornusa==2 & citizen==.
tab citizen, m

*All Foreign Born v. All US Born
*0 = US born, 1 = foreign born
gen nativity=.
replace nativity=0 if bornusa==1
replace nativity=0 if bornusa==2
replace nativity=1 if bornusa==3
replace nativity=. if bornusa==8
replace nativity=. if bornusa==9
label define nativity_lab 1 "Foreign Born"  0 "U.S. Born" , replace
label values nativity nativity_lab
label variable nativity "Foreign Born vs U.S. Born"
tab nativity, missing

gen missing_nativity = 0
replace missing_nativity = 1 if nativity==.
tab missing_nativity

tab citizen nativity, col m

*legal status
*1=US BORN CITIZENS
*2=NATURALIZED CITIZENS
*3=PERMANENT RESIDENTS
*4=NOT A CITZEN/RESIDENT

gen legal_status=.
replace legal_status=1 if nativity==0   
replace legal_status=2 if citizen==1 & nativity==1

replace legal_status=3 if citizen==0 & nativity==1 & greencard==1
replace legal_status=3 if citizen==. & nativity==1 & greencard==1

replace legal_status=4 if citizen==0 & nativity==1 & greencard==2
replace legal_status=4 if citizen==0 & nativity==1 & greencard==8
replace legal_status=4 if citizen==0 & nativity==1 & greencard==9
replace legal_status=4 if citizen==. & nativity==1 & greencard==8
replace legal_status=4 if citizen==. & nativity==1 & greencard==9
replace legal_status=4 if citizen==. & nativity==1 & greencard==2

label define l_status 1 "U.S.-born Citizens"  2 "Naturalized Citizens" 3 "Lawful Permanent Residents" 4 "Likely-Undocumented"  
label values legal_status l_status
label variable legal_status "Legal Status (4 level)"
tab legal_status, missing

replace citizen = 1 if legal_status==1 & citizen==.
replace citizen =1 if legal_status==2 & citizen==.
replace citizen =0 if legal_status==3 & citizen==.
replace citizen =0 if legal_status==4 & citizen==.
tab citizen, m

replace legal_status = 1 if citizen==1 & legal_status==.

tab citizen legal_status, col m


*qn27: question of interest

fre qn27
recode qn27 (1=1 "Yes") (2=0 "No")  (8 = .), gen(deported)
label variable deported "Personally know someone detained or deported"
fre deported

fre deported
replace deported = . if deported==8 | deported==9

rename weight svy_weight
 
keep citizen legal_status deported svy_weight id svy_year 

save "2011_clean.dta", replace




*2012 data
use "2012.dta", clear

***Rename Variables ------------------------
* Case Identifier (id)

gen svy_year = 2012

* Main Dependent Variable
	*Not available


** Main Independent Variables
*All Citizens v. All Noncitizens

fre qn9
recode qn9 (1=1 "U.S. Citizens") (2=0 "Noncitizens")  (8/9 = .), gen(citizen)
label variable citizen "Are you a citizen of the US?"
fre citizen 
rename qn4 bornusa
fre bornusa
rename qn77 greencard
fre greencard
tab citizen, m

replace citizen = 1 if bornusa==1 | bornusa==2 & citizen==.
tab citizen, m

*All Foreign Born v. All US Born
*0 = US born, 1 = foreign born
gen nativity=.
replace nativity=0 if bornusa==1
replace nativity=0 if bornusa==2
replace nativity=1 if bornusa==3
replace nativity=. if bornusa==8
replace nativity=. if bornusa==9
label define nativity_lab 1 "Foreign Born"  0 "U.S. Born" , replace
label values nativity nativity_lab
label variable nativity "Foreign Born vs U.S. Born"
tab nativity, missing

gen missing_nativity = 0
replace missing_nativity = 1 if nativity==.
tab missing_nativity

tab citizen nativity, col m

*legal status
*1=US BORN CITIZENS
*2=NATURALIZED CITIZENS
*3=PERMANENT RESIDENTS
*4=NOT A CITZEN/RESIDENT

gen legal_status=.
replace legal_status=1 if nativity==0   
replace legal_status=2 if citizen==1 & nativity==1

replace legal_status=3 if citizen==0 & nativity==1 & greencard==1
replace legal_status=3 if citizen==. & nativity==1 & greencard==1

replace legal_status=4 if citizen==0 & nativity==1 & greencard==2
replace legal_status=4 if citizen==0 & nativity==1 & greencard==8
replace legal_status=4 if citizen==0 & nativity==1 & greencard==9
replace legal_status=4 if citizen==. & nativity==1 & greencard==8
replace legal_status=4 if citizen==. & nativity==1 & greencard==9
replace legal_status=4 if citizen==. & nativity==1 & greencard==2

label define l_status 1 "U.S.-born Citizens"  2 "Naturalized Citizens" 3 "Lawful Permanent Residents" 4 "Likely-Undocumented"  
label values legal_status l_status
label variable legal_status "Legal Status (4 level)"
tab legal_status, missing

replace citizen = 1 if legal_status==1 & citizen==.
replace citizen =1 if legal_status==2 & citizen==.
replace citizen =0 if legal_status==3 & citizen==.
replace citizen =0 if legal_status==4 & citizen==.
tab citizen, m

replace legal_status = 1 if citizen==1 & legal_status==.

tab citizen legal_status, col m


*qn34: question of interest

fre qn34
recode qn34 (1=1 "Yes") (2=0 "No")  (8 = .), gen(deported)
label variable deported "Personally know someone detained or deported"
fre deported

rename weight svy_weight
 

fre qn32
recode qn32 (1=1 "Yes") (2=0 "No")  (3=1 "Yes") (8=.), gen(applied)
label variable applied "Do you know someone who has applied for DACA"

*qn32 applied for DACA

keep  citizen legal_status deported applied svy_weight id svy_year

save "2012_clean.dta", replace




*2014 data

use "2014.dta", clear
rename caseid id
destring id, replace
gen svy_year = 2014

*All Citizens v. All Noncitizens

tab q9
tab q9, nolabel
recode q9 (1=1 "U.S. Citizens") (2=0 "Noncitizens")  (8/9 = .), gen(citizen)
label variable citizen "Are you a citizen of the US?"
tab citizen,m 
rename nativity1 bornusa
rename q70 greencard
tab citizen, m

replace citizen = 1 if bornusa==1 
tab citizen, m


*Specific Legal Status
	*1=US BORN CITIZENS
	*2=NATURALIZED CITIZENS
	*3=PERMANENT RESIDENTS
	*4=NOT A CITZEN/RESIDENT

gen legal_status=.
replace legal_status=1 if nativity==1 
replace legal_status=2 if citizen==1 & nativity==2

replace legal_status=3 if citizen==0 & nativity==2 & greencard==1
replace legal_status=3 if citizen==. & nativity==2 & greencard==1

replace legal_status=4 if citizen==0 & nativity==2 & greencard==2
replace legal_status=4 if citizen==0 & nativity==2 & greencard==8
replace legal_status=4 if citizen==0 & nativity==2 & greencard==9
replace legal_status=4 if citizen==. & nativity==2 & greencard==8
replace legal_status=4 if citizen==. & nativity==2 & greencard==9
replace legal_status=4 if citizen==. & nativity==2 & greencard==2

label define l_status 1 "U.S.-born Citizens"  2 "Naturalized Citizens" 3 "Lawful Permanent Residents" 4 "Likely-Undocumented"  
label values legal_status l_status
label variable legal_status "Legal Status (4 level)"
tab legal_status, missing


*q31: question of interest

fre q31
recode q31 (1=1 "Yes") (2=0 "No")  (8/9 = .), gen(deported)
label variable deported "Personally know someone detained or deported"
fre deported

rename weight svy_weight

keep  citizen legal_status deported  svy_weight id svy_year 

save "2014_clean.dta", replace



 use "2014_clean.dta", clear
append using "2012_clean.dta"
append using "2011_clean.dta"
append using "2010_clean.dta"

save "knows_deported.dta", replace


svyset id [pweight = svy_weight] 

recode legal_status (1=3 "U.S.-born Citizens") (2=2 "Naturalized Citizens") (3=1 "Lawful Permanent Residents") (4=0 "Likely-Undocumented Immigrants"), gen(legal_order)
label variable legal_order "Legal Status (ordered)"
tab legal_order, m

svy: logistic worry2 citizen if svy_year==2010
margins , vce(unconditional)
svy: logistic worry2 citizen##deported if svy_year==2010
margins  citizen#deported , vce(unconditional)
margins  citizen#deported , pwcompare

svy: logistic worry2 legal_order##deported if svy_year==2010
margins legal_order#deported , vce(unconditional)
marginsplot, by(legal_order)
margins legal_order#deported , pwcompare

svy: logistic deported i.legal_order if svy_year==2010
margins i.legal_order, vce(unconditional)

svy, over(legal_status): proportion applied if svy_year==2012
svy: logistic applied i.legal_status if svy_year==2012
margins i.legal_status, vce(unconditional)
margins i.legal_status, pwcompare
*naturalized and US-born as likely to know someone who applied for DACA in 2012
svy: logistic applied i.legal_status##i.deported if svy_year==2012
margins i.legal_status#i.deported
margins i.legal_status#i.deported if legal_status==1, pwcompare
*USB in 2012 who know someone who's been deported also more likely to know someone who has appleid for DACA
margins i.legal_status#i.deported if legal_status==2, pwcompare
*same for naturalized citizens
margins i.legal_status#i.deported if legal_status==3, pwcompare
*same for LPRs
margins i.legal_status#i.deported if legal_status==4, pwcompare
*same for undocumented
margins i.legal_status#i.deported if legal_status==1 | legal_status==2, pwcompare

svy: logistic applied i.legal_order##i.deported if svy_year==2012
margins i.legal_order#i.deported
marginsplot, by(legal_order)

svy, over(legal_status): proportion deported if svy_year==2010
svy, over(legal_status): proportion deported if svy_year==2011
svy, over(legal_status): proportion deported if svy_year==2012
svy, over(legal_status): proportion deported if svy_year==2014


svy: logistic deported i.citizen##i.svy_year
margins i.citizen#i.svy_year
marginsplot, by(citizen)


svy: logistic deported i.legal_status##i.svy_year
margins i.legal_status#i.svy_year
marginsplot, by(legal_status)
margins i.legal_status#i.svy_year if legal_status==1, pwcompare
*US born as likely across time to know someone who has been detained/deported
margins i.legal_status#i.svy_year if legal_status==2, pwcompare
*naturalized citizens as likely to know someone who has been deported across time
margins i.legal_status#i.svy_year if legal_status==3, pwcompare
*same for LPRs
margins i.legal_status#i.svy_year if legal_status==4, pwcompare
*same for likely undocumented

svy: logistic deported i.legal_order##i.svy_year
margins i.legal_order#i.svy_year
marginsplot, by(legal_order)

svy: logistic worry2 i.legal_status##i.deported if svy_year==2010
margins i.legal_status#i.deported
margins i.legal_status#i.deported if legal_status==1, pwcompare
*in 2010, US born citizens more likely to fear deportation when they know someone who has been deported; contrast: 0.244974 (0.13, 0.36); margins for USborn is 0.50 (0.40, 0.60)
margins i.legal_status#i.deported if legal_status==2, pwcompare
*in 2010, naturalized citizens more likely to fear deported when they know someone who has been deported; contrast 0.39 (0.28, 0.50); margin is 0.87 (0.79, 0.95)
margins i.legal_status#i.deported if legal_status==3, pwcompare
*same for LPRs; contrast 0.15 (0.01, 0.28); margin is 0.80 (0.71, 0.90)
margins i.legal_status#i.deported if legal_status==4, pwcompare
*undocumented no more likely to fear deportation if they know someone who has been deported; contrast is 0.03 (-0.08, 0.13)



svy: logistic worry2 i.legal_order##i.deported if svy_year==2010
margins i.legal_order#i.deported
marginsplot, by(legal_order)
